package b1.controls.basic
{
	import b1.conifg.*;
	import b1.net.*;
	import b1.players.*;
	import b1.utils.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.Timer;
	import flash.filters.*;

	public class AnimalMc extends Sprite
	{
		public var animalId:int;
		//动物背后的图层，披风装备需要放在动物背后
		private var backLayer:Sprite;
		private var currentMc:MovieClip;
		private var walkMc:MovieClip;
		private var idleMc:MovieClip;
		private var hitheadMc:MovieClip;
		private var _status:int;
		private var leftFlag:Boolean;
		private var _height:int;
		private var walkEquips:Object;
		private var idleEquips:Object;
		private var idleBackEquips:Object;
		
		//status: 0, 正面休闲; 1, 左方向走; 2, 右方向走; 3, 左站立; 4, 右站立; 5, 缩一下脖子;
		public function AnimalMc(animalId:int, height:int = 100, status:int = 0)
		{
			super();
			this.animalId = animalId;
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var animalInfo:AnimalInfo = animals[animalId];
			this._height = height;
			this.leftFlag = false;
			this.walkEquips = new Object();
			this.idleEquips = new Object();
			this.idleBackEquips = new Object();
			
			backLayer = new Sprite();
			addChild(backLayer);
			
			var AnimalMovieClip:Class = ResourceHelper.getInstance().getClassByName("Walk" + animalInfo.animalIndex);
			if (AnimalMovieClip != null)
			{
				walkMc = new AnimalMovieClip();
				walkMc.stop();
				walkMc.height = height;
				walkMc.scaleX = walkMc.scaleY;
				walkMc.visible = false;
				addChild(walkMc);
			}

			AnimalMovieClip = ResourceHelper.getInstance().getClassByName("Idle" + animalInfo.animalIndex);
			if (AnimalMovieClip != null)
			{
				idleMc = new AnimalMovieClip();
				idleMc.height = height;
				idleMc.scaleX = idleMc.scaleY;
				idleMc.stop();
				idleMc.visible = false;
				addChild(idleMc);
				var randFrame:int = Math.random() * idleMc.totalFrames + 1;
				idleMc.gotoAndStop(randFrame);
				currentMc = idleMc;
				currentMc.visible = true;
			}
			
			AnimalMovieClip = ResourceHelper.getInstance().getClassByName("HitHead" + animalInfo.animalIndex);
			if (AnimalMovieClip != null)
			{
				hitheadMc = new AnimalMovieClip();
				hitheadMc.height = height;
				hitheadMc.scaleX = hitheadMc.scaleY;
				hitheadMc.stop();
				hitheadMc.visible = false;
				addChild(hitheadMc);
			}
			
			refreshEquips();
			
			var timer:Timer = new Timer(300);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER, onTick);
			
			this.status = status;
		}
		
		public function refreshEquips():void
		{
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[animalId];
			for (var i:int = 0; i < 4; i++)
			{
				getOffEquip(i);
			} 
			for (var position:String in animalInfo.equips)
			{
				var equip:EquipInfo = animalInfo.equips[position];
				getOnEquip(equip.objectIndex);
			}
		}
		
		//穿上装备
		private function getOnEquip(objectIndex:int):void
		{
			var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
			getOffEquip(equipConfig.position);
			//加载新的装备
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[animalId];
			var WalkEquip:Class;
			if (equipConfig.baseIndex >= 0)
			{
				WalkEquip = ResourceHelper.getInstance().getClassByName("Walk" + animalInfo.animalIndex + "Equip" + equipConfig.baseIndex);
			}
			else
			{
				WalkEquip = ResourceHelper.getInstance().getClassByName("Walk" + animalInfo.animalIndex + "Equip" + objectIndex);
			}
			if (WalkEquip != null)
			{
				var walkEquip:MovieClip = new WalkEquip();
				walkEquip.scaleX = walkMc.scaleY;
				walkEquip.scaleY = walkMc.scaleY;
				walkEquips[equipConfig.position] = walkEquip; 
				if (equipConfig.color != 0)
				{
					var gradientGlow:GradientGlowFilter;
				
					gradientGlow = new GradientGlowFilter();
					gradientGlow.distance = 0;
					gradientGlow.angle = 45;
					if (equipConfig.color > 0)
						gradientGlow.colors = [0xFFFFFF, Config.equipColor2[equipConfig.color]];
					gradientGlow.alphas = [0, 1];
					gradientGlow.ratios = [0, 255];
					gradientGlow.blurX = 20;
					gradientGlow.blurY = 20;
					gradientGlow.strength = 2;
					gradientGlow.quality = BitmapFilterQuality.HIGH;
					gradientGlow.type = BitmapFilterType.OUTER;
					walkEquip.filters = [gradientGlow];
				}
				addChild(walkEquip);
			}
			if (equipConfig.baseIndex >= 0)
			{
				var IdleEquip:Class = ResourceHelper.getInstance().getClassByName("Idle" + animalInfo.animalIndex + "Equip" + equipConfig.baseIndex);
			}
			else
			{
				IdleEquip = ResourceHelper.getInstance().getClassByName("Idle" + animalInfo.animalIndex + "Equip" + objectIndex);
			}
			if (IdleEquip != null)
			{
				var idleEquip:MovieClip = new IdleEquip();
				idleEquips[equipConfig.position] = idleEquip; 
				idleEquip.scaleX = idleMc.scaleY;
				idleEquip.scaleY = idleMc.scaleY;
				gradientGlow = new GradientGlowFilter();
				gradientGlow.distance = 0;
				gradientGlow.angle = 45;
				if (equipConfig.color > 0)
					gradientGlow.colors = [0xFFFFFF, Config.equipColor2[equipConfig.color]];
				gradientGlow.alphas = [0, 1];
				gradientGlow.ratios = [0, 255];
				gradientGlow.blurX = 20;
				gradientGlow.blurY = 20;
				gradientGlow.strength = 2;
				gradientGlow.quality = BitmapFilterQuality.HIGH;
				gradientGlow.type = BitmapFilterType.OUTER;
				if(equipConfig.color != 0)
				{
					idleEquip.filters = [gradientGlow];
				}
				addChild(idleEquip);
			}
			//特殊处理位于动物背后的装备，如：披风
			if (equipConfig.position == 2)
			{
				var IdleBackEquip:Class
				if (equipConfig.baseIndex >= 0)
				{
					IdleBackEquip = ResourceHelper.getInstance().getClassByName("Idle" + animalInfo.animalIndex + "BackEquip" + equipConfig.baseIndex);
				}
				else
				{
					IdleBackEquip = ResourceHelper.getInstance().getClassByName("Idle" + animalInfo.animalIndex + "BackEquip" + objectIndex);
				}
				if (IdleBackEquip != null)
				{
					var idleBackEquip:MovieClip = new IdleBackEquip();
					idleBackEquips[equipConfig.position] = idleBackEquip; 
					idleBackEquip.scaleX = idleMc.scaleY;
					idleBackEquip.scaleY = idleMc.scaleY;
					gradientGlow = new GradientGlowFilter();
					gradientGlow.distance = 0;
					gradientGlow.angle = 45;
					if (equipConfig.color > 0)
						gradientGlow.colors = [0xFFFFFF, Config.equipColor2[equipConfig.color]];
					gradientGlow.alphas = [0, 1];
					gradientGlow.ratios = [0, 255];
					gradientGlow.blurX = 20;
					gradientGlow.blurY = 20;
					gradientGlow.strength = 2;
					gradientGlow.quality = BitmapFilterQuality.HIGH;
					gradientGlow.type = BitmapFilterType.OUTER;
					if(equipConfig.color != 0)
					{
						idleBackEquip.filters = [gradientGlow];
					}
					backLayer.addChild(idleBackEquip);
				}
			}
			adjustEquips();
		}
		
		//脱下装备
		private function getOffEquip(position:int):void
		{
			//卸载同一部位的已有装备
			var oldWalkEquip:MovieClip = walkEquips[position];
			if (oldWalkEquip != null && oldWalkEquip.parent != null)
				oldWalkEquip.parent.removeChild(oldWalkEquip);
			var oldIdleEquip:MovieClip = idleEquips[position];
			if (oldIdleEquip != null && oldIdleEquip.parent != null)
				oldIdleEquip.parent.removeChild(oldIdleEquip);
			var oldIdleBackEquip:MovieClip = idleBackEquips[position];
			if (oldIdleBackEquip != null && oldIdleBackEquip.parent != null)
				oldIdleBackEquip.parent.removeChild(oldIdleBackEquip);
			delete walkEquips[position];
			delete idleEquips[position];
			delete idleBackEquips[position];
		}
		
		//0, 正面休闲; 1, 左方向走; 2, 右方向走; 3, 左站立; 4, 右站立; 5, 缩一下脖子;
		public function set status(newStatus:int):void
		{
			if (_status != newStatus)
			{
				_status = newStatus;
				if (currentMc != null)
					currentMc.visible = false;
				if (_status == 0)
				{
					currentMc = idleMc;
					var randFrame:int = Math.random() * currentMc.totalFrames + 1;
					currentMc.gotoAndStop(randFrame);
					leftFlag = false;
				}
				else if (_status == 1)
				{
					currentMc = walkMc;
					leftFlag = true;
				}
				else if (_status == 2)
				{
					currentMc = walkMc;
					leftFlag = false;
				}
				else if (_status == 3)
				{
					currentMc = walkMc;	
					currentMc.gotoAndStop(1);
					leftFlag = true;
				}
				else if (_status == 4)
				{
					currentMc = walkMc;	
					currentMc.gotoAndStop(1);
					leftFlag = false;
				}
				else if (_status == 5)
				{
					currentMc = hitheadMc;	
					currentMc.gotoAndStop(1);
				}
				currentMc.visible = true;
				//设置左侧和右侧的镜像变换
				if (leftFlag)
				{
//					currentMc.x = currentMc.width;
					currentMc.scaleX = -currentMc.scaleY;
				}
				else
				{
//					currentMc.x = 0;
					currentMc.scaleX = currentMc.scaleY;
				}
				adjustEquips();
			}
		}
		
		public function adjustEquips():void
		{
			if (_status == 0)
			{
				for (var position:String in walkEquips)
				{
					walkEquips[position].visible = false;
				}
				for (position in idleEquips)
				{
					idleEquips[position].visible = true;
				}
				for (position in idleBackEquips)
				{
					idleBackEquips[position].visible = true;
				}
			}
			else
			{
				for (position in walkEquips)
				{
					walkEquips[position].visible = true;
				}
				for (position in idleEquips)
				{
					idleEquips[position].visible = false;
				}
				for (position in idleBackEquips)
				{
					idleBackEquips[position].visible = false;
				}
			}
			if (leftFlag)
			{
				for (position in walkEquips)
				{
					walkEquips[position].scaleX = -walkMc.scaleY;
				}
				for (position in idleEquips)
				{
					idleEquips[position].scaleX = -idleMc.scaleY;
				}
				for (position in idleBackEquips)
				{
					idleBackEquips[position].scaleX = -idleMc.scaleY;
				}
			}
			else
			{
				for (position in walkEquips)
				{
					walkEquips[position].scaleX = walkMc.scaleY;
				}
				for (position in idleEquips)
				{
					idleEquips[position].scaleX = idleMc.scaleY;
				}
				for (position in idleBackEquips)
				{
					idleBackEquips[position].scaleX = idleMc.scaleY;
				}
			}
			//装备速度
			for (position in walkEquips)
			{
				walkEquips[position].gotoAndStop(currentMc.currentFrame);
			}
			for (position in idleEquips)
			{
				idleEquips[position].gotoAndStop(currentMc.currentFrame);
			}
			for (position in idleBackEquips)
			{
				idleBackEquips[position].gotoAndStop(currentMc.currentFrame);
			}
		}
		
		public function get animalHeight():Number
		{
			return _height;
		}
		
		public function get animalWidth():Number
		{
			return currentMc.width;
		}
		
		public function get status():int
		{
			return _status;
		}
		
		public function onTick(event:Event):void
		{

			if (_status == 3 || _status == 4)
			{
				return;
			}
			else if (_status == 5 || _status == 6)
			{
				if (currentMc.currentFrame < currentMc.totalFrames)
				{
					currentMc.gotoAndStop(currentMc.currentFrame % currentMc.totalFrames + 1);
				}
				else
				{
					if (leftFlag)
					{
						status = 3;
					}
					else
					{
						status = 4;
					}
				}
			}
			else
			{
				currentMc.gotoAndStop(currentMc.currentFrame % currentMc.totalFrames + 1);
			}
			//装备速度
			for (var position:String in walkEquips)
			{
				walkEquips[position].gotoAndStop(currentMc.currentFrame);
			}
			for (position in idleEquips)
			{
				idleEquips[position].gotoAndStop(currentMc.currentFrame);
			}
			for (position in idleBackEquips)
			{
				idleBackEquips[position].gotoAndStop(currentMc.currentFrame);
			}
		}
	}
}